// pages/tongcheng/index.js
const { fetch, newFetch } = require('../../utils/fetch')
import {
  Base
} from '../../utils/base.js';
var base = new Base();


Page({
  /**
   * 页面的初始数据
   */
  data: {
    uploadimgs: [1,2,3,4],
    specificarea:null,
    topicdata: {},
    commonList: [],
    iscollection: '',
    firstFlag: true,
    reply_text: '',
    detailid: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {
      id
    } = options
    this.setData({
      detailid: id
    })
    if(wx.getStorageSync('specificarea')) {
      this.setData({
        specificarea:wx.getStorageSync('specificarea')
      })
    }
    this.getData()
  },

  getData() {
    fetch(`wx/tribe/${this.data.specificarea}/gettribeByid`, 'POST', {
      tribeid: this.data.detailid
    }).then(res => {
      console.log(res, '路上看到房价')
      this.setData({
        topicdata: res,
        uploadimgs: res.photohead ? res.photohead.split(',') : res.photohead
      })
      this.getReplyList(this.data.topicdata.id, this.data.topicdata.area)
    })
  },

  // 获取评论
  getReplyList(tribeid, area) {
    fetch(`wx/reply/${this.data.specificarea}/list`, 'POST', {
      type: 4,
      typeid: tribeid,
      area: area
    }).then(res => {
      this.setData({
        commonList: res.list
      })
    })
  },
  onClickLike() {
    newFetch(`wx/memberzan/${this.data.specificarea}/addZan`, 'POST', {
      // tribeid: this.data.topicdata.id,
      type: 4,
      typeId: this.data.topicdata.id,
      memberId: wx.getStorageSync('userInfo').id
    }).then(res => {
      if(res.code == 200) {
           wx.showToast({
            title: '点赞成功',
            icon: 'none',
            duration: 1000,
            mask: true
          })
      }
      this.setData({
        firstFlag:true,
        data: res,
        imgs: res.photohead ? res.photohead.split(',') : res.photohead
      })
      this.getData()
      // this.getReplyList(this.data.data.tribeid, this.data.data.area)
    })
  },

  
  onChange(event) {
    this.setData({
      reply_text: event.detail.value
    })
    // event.detail 为当前输入的值
  },

  onConfirm() {
    console.log(this.data.topicdata, '收到了咖啡机')
    if (this.data.reply_text == '') {
      base.showmsg('请输入评论');
      return;
    }
    wx.showLoading({
        title: '提交中...'
    })
    newFetch(`wx/reply/${this.data.specificarea}/fabu`, 'POST', {
      content: this.data.reply_text,
      memberid: wx.getStorageSync('userInfo').id,
      type: 4,
      typeid: this.data.topicdata.id,
      area: this.data.specificarea,
    }).then(res => {
      console.log(res, '思路打开房间')
      if(res.code == 200) {
        base.showmsg('评论成功');
        wx.hideLoading()
        this.getReplyList(this.data.topicdata.id, this.data.topicdata.area)
        this.setData({
          reply_text: ''
        })
      } else {
        base.showmsg(res.msg);
      }
    })
  },

  previewImage(e) {
    let index = e.target.dataset.index
    let {
      uploadimgs
    } = this.data
    wx.previewImage({
      current: uploadimgs[index],
      urls: uploadimgs
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})